<template>
  <div>
    <c-table-box ref="cTableBox" hide-func checkbox :other-filters="otherFilters" :table-config="tableConfig" @selection-change="selectionChange">
      <template #btn>
        <c-permission v-if="type==='all'" code="all">
          <Abnormal :sel-data="selections" opt-code="submitException" @on-success="refreshTableData" />
          <Visualize :sel-data="selections" opt-code="change" @on-success="refreshTableData" />
          <c-button type="primary" opt-code="import" @click="importVisible=true">导入</c-button>
          <c-button type="primary" opt-code="export" @click="exportVisible=true">导出</c-button>
          <c-button type="primary" opt-code="ReportExport" @click="exportReportVisible=true">报表导出</c-button>
          <c-button type="primary" opt-code="termination" @click="handleTerminal">终止</c-button>
        </c-permission>
        <c-permision v-else-if="type==='selfChecking'" code="selfChecking">
          <self-checking :sel-data="selections" opt-code="selfCheck" @on-success="refreshTableData" />
        </c-permision>
        <c-permision v-else-if="type==='selfCheckAudit'" code="selfCheckAudit">
          <check-audit :sel-data="selections" opt-code="checkAudit" @on-success="refreshTableData" />
        </c-permision>
        <c-permision v-else-if="type==='display'" code="display">
          <Display :sel-data="selections" opt-code="replay" @on-success="refreshTableData" />
          <c-button type="primary" opt-code="export" @click="exportVisible=true">导出</c-button>
        </c-permision>
        <c-permision v-else-if="type==='initialSurvey'" code="initialSurvey">
          <Check :sel-data="selections" opt-code="initialSurvey" @on-success="refreshTableData" />
          <c-button type="primary" opt-code="export" @click="exportVisible=true">导出</c-button>
        </c-permision>
        <c-permission v-else-if="type==='recheck'" code="recheck">
          <Check recheck :sel-data="selections" opt-code="recheck" @on-success="refreshTableData" />
          <c-button type="primary" opt-code="export" @click="exportVisible=true">导出</c-button>
        </c-permission>
      </template>
    </c-table-box>
    <import-files
      v-if="importVisible"
      title="导入"
      :visible.sync="importVisible"
      :config="fileImportConfig"
      @success="handleImportSuccess"
    />
    <export-files
      v-if="exportVisible"
      title="导出"
      :config="exportConfig"
      :visible.sync="exportVisible"
      @success="handleExportSuccess"
    />
    <export-files
      v-if="exportReportVisible"
      title="报表导出"
      :config="exportReportConfig"
      :visible.sync="exportReportVisible"
      @success="handleExportSuccess"
    />
  </div>
</template>
<script>
import checkAudit from './selfCheckAudit'
import selfChecking from './selfChecking'
import { otherFiltersItem } from '@/utils'
import Abnormal from './abnormal'
import Visualize from './visualize'
import Display from './display'
import Check from './check'
import { updateData } from '@/api/check'
// import Recheck from './recheck'
export default {
  name: 'List',
  components: {
    Abnormal,
    Visualize,
    Display,
    Check,
    selfChecking,
    checkAudit
    // Recheck
  },
  props: {
    type: {
      type: String,
      default: ''
    },
    activeType: {
      type: String,
      default: ''
    },
    serachFilters: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      importVisible: false,
      exportVisible: false,
      exportReportVisible: false,
      tableConfig: {
        interfacePath: '/slm-admin/api/v1/shopSoftwearDisplay/selectPage',
        hideFuncBar: true,
        sort: 'createTime',
        order: 'desc',
        apiHeader: {
          'Permission-App': 'slm'
        },
        setting: [
          {
            prop: 'constCode',
            label: '门店建设项目号',
            width: 180
          },
          {
            prop: 'softwearTaskCode',
            label: '软装陈列任务号',
            width: 200,
            className: 'btn-green',
            isButton: true,
            clickFun: this.toDetail
          },
          {
            prop: 'marketType',
            label: '报图类型',
            width: 120,
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'MARKET_TYPE'
            }
          },
          {
            prop: 'constType',
            label: '门店建设类型',
            width: 120,
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'CONST_TYPE'
            }
          },
          {
            prop: 'shopName',
            label: '所属门店',
            width: 120
          },
          {
            prop: 'officeName',
            label: '所属办事处',
            width: 120
          },
          {
            prop: 'range',
            label: '装修范围',
            width: 120,
            propEnum: {
              appCode: 'slm',
              domainCode: 'marketing',
              dictTypeCode: 'DECORATION_RANGE'
            }
          },
          {
            prop: 'schedule',
            label: '软装陈列进度',
            width: 120,
            propEnum: {
              appCode: 'slm',
              domainCode: 'marketing',
              dictTypeCode: 'SHOP_SOFTWEAR_DISPLAY_SCHEDULE'
            }
          },
          {
            prop: 'steps',
            label: '软装陈列步骤',
            width: 120,
            propEnum: {
              appCode: 'slm',
              domainCode: 'marketing',
              dictTypeCode: 'SHOP_SOFTWEAR_DISPLAY_STEPS'
            }
          },
          {
            prop: 'state',
            label: '软装状态',
            width: 120,
            propEnum: {
              appCode: 'slm',
              domainCode: 'marketing',
              dictTypeCode: 'SHOP_DECORATION_STATE'
            }
          },
          {
            prop: 'decorationTaskCode',
            label: '装修任务号',
            width: 120
          },
          {
            prop: 'formSupervisorName',
            label: '形象督导'
          },
          {
            prop: 'displayStartTime',
            label: '软装陈列开始时间',
            width: 140,
            formatData: this.$parseTimeYMD('displayStartTime')
          }
        ]
      },
      typeFilters: [],
      selections: []
    }
  },
  computed: {
    otherFilters() {
      return [...this.serachFilters, ...this.typeFilters]
    },
    name() {
      let str = ''
      switch (this.type) {
        case 'all':
          str = '软装陈列管理'
          break
        case 'display':
          str = '软装陈列'
          break
        case 'initialSurvey':
          str = '软装初检'
          break
        case 'recheck':
          str = '软装复检'
          break
        default:
          break
      }
      return str
    },
    exportConfig() {
      return [{
        code: 'export',
        name: this.name,
        dataApi: '/slm-admin/api/v1/shopSoftwearDisplay/export',
        selectedRows: this.selections,
        paramsRaw: {},
        filtersRaw: this.otherFilters,
        queryField: 'id',
        headers: {
          'Permission-perCode': 'softdecorate'
          // 'Permission-App': 'slm'
        }
      }]
    },
    exportReportConfig() {
      return [{
        code: 'export',
        name: '门店信息',
        dataApi: '/slm-admin/api/v1/shopSoftwearDisplay/exportShopInfo',
        selectedRows: this.selections,
        paramsRaw: {},
        filtersRaw: this.otherFilters,
        queryField: 'id',
        headers: {
          'Permission-perCode': 'softdecorate'
          // 'Permission-App': 'slm'
        }
      }]
    },
    fileImportConfig() {
      return [{
        code: 'import',
        name: '软装陈列管理',
        apiUrl: '/slm-admin/api/v1/shopSoftwearDisplay/importExcel',
        selectedRows: this.selections,
        paramsRaw: {},
        filtersRaw: this.otherFilters
      }]
    }
  },
  watch: {
    type: {
      handler(v) {
        this.setOtherFilters(v)
      },
      immediate: true
    },
    serachFilters: {
      handler(v) {
        if (this.activeType !== this.type) return
        this.refreshTableData()
      }
    }
  },
  created() {
  },
  activated() {
    if (this.ready && this.refreshTableData) {
      this.refreshTableData()
    }
  },
  deactivated() {
    this.ready = true
  },
  methods: {
    // 设置过滤条件
    setOtherFilters(v) {
      switch (v) {
        case 'all':
          this.typeFilters = []
          break
        case 'selfChecking':
          this.typeFilters = [
            otherFiltersItem('steps', 'I1', '='),
            otherFiltersItem('state', 'A', '='),
            otherFiltersItem('isEnd', '0', '=')
          ]
          break
        case 'selfCheckAudit':
          this.typeFilters = [
            otherFiltersItem('steps', 'I2', '='),
            // 除全部页签，其余全展示正常
            otherFiltersItem('state', 'A', '='),
            otherFiltersItem('isEnd', '0', '=')
          ]
          break
        case 'display':
          this.typeFilters = [
            otherFiltersItem('steps', 'A1', '='),
            // 除全部页签，其余全展示正常
            otherFiltersItem('state', 'A', '='),
            otherFiltersItem('isEnd', '0', '=')
          ]
          break
        case 'initialSurvey':
          this.typeFilters = [
            otherFiltersItem('steps', 'B1', '='),
            // 除全部页签，其余全展示正常
            otherFiltersItem('state', 'A', '='),
            otherFiltersItem('isEnd', '0', '=')
          ]
          break
        case 'recheck':
          this.typeFilters = [
            otherFiltersItem('steps', 'C1', '='),
            // 除全部页签，其余全展示正常
            otherFiltersItem('state', 'A', '='),
            otherFiltersItem('isEnd', '0', '=')
          ]
          break
      }
    },
    selectionChange(v) {
      this.selections = v
    },
    refreshTableData(v = 'onPage') {
      this.$nextTick(() => {
        this.$refs.cTableBox.initTableData(v)
      })
    },
    // 跳转
    clickToRouterHandle(name) {
      this.$router.push({ name })
    },
    // 详情
    toDetail(row) {
      this.$router.push({ name: 'SoftDecorateDetail', query: { id: row.id, taskCode: row.decorationTaskCode, softTaskCode: row.softwearTaskCode }})
    },
    // 导出成功
    handleExportSuccess() {
      this.$message.success('导出成功！')
    },
    // 导入成功
    handleImportSuccess() {
      this.$message.success('导入成功！')
    },
    // 终止数据
    handleTerminal() {
      if (!this.singleJudge(this.selections, '终止', true)) {
        return
      }
      if (!this.selections[0].schedule === 'A' && !this.selections[0].steps === 'A1' && this.selections[0].state === 'C') {
        this.$message.warning('装修进度为【陈列】才能终止')
        return
      }
      this.$prompt('', '备注：', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPlaceholder: '请输入备注'
      }).then(({ value }) => {
        const data = {
          id: this.selections[0].id,
          remark: value,
          state: 'C'
        }
        updateData(data).then(res => {
          if (res.success) {
            this.$message.success('终止成功')
            this.refreshTableData()
          }
        })
      })
    }
  }
}
</script>
